package com.example.taluo.entity.dto.gamedto;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/*** @Author supersaiya
 * @Description TODO

 * @Date 2025/2/18 16:56

 * @Version 1.0*/
@Getter
@Setter
@ToString
public class GameState {
    private ConcurrentHashMap<String, PlayerState> players; // 玩家状态映射
    private String currentPlayerId; // 当前出牌玩家的 ID
    private boolean gameEnded; // 游戏是否结束
    private String winnerId; // 胜利玩家的 ID

    public GameState() {
        this.players = new ConcurrentHashMap<>();
    }

    public void addPlayer(String playerId, PlayerState playerState) {
        players.put(playerId, playerState);
    }

    public void removePlayer(String playerId) {
        players.remove(playerId);
    }

    public PlayerState getPlayer(String playerId) {
        return players.get(playerId);
    }

    public String getCurrentPlayerId() {
        return currentPlayerId;
    }

    public void setCurrentPlayerId(String currentPlayerId) {
        this.currentPlayerId = currentPlayerId;
    }

    public boolean isGameEnded() {
        return gameEnded;
    }

    public void setGameEnded(boolean gameEnded) {
        this.gameEnded = gameEnded;
    }

    public String getWinnerId() {
        return winnerId;
    }

    public void setWinnerId(String winnerId) {
        this.winnerId = winnerId;
    }

    public ConcurrentHashMap<String, PlayerState> getPlayers() {
        return players;
    }


    // 方便广播的方法，将游戏状态转换为 JSON 或其他格式
    public String toJson() {
        // 使用 JSON 库序列化 players, currentPlayerId, gameEnded 等状态
        return "{...}";
    }

    //判断是不是当前回合用户id
    public boolean isCurrentPlayer(String playerId) {
        return playerId==currentPlayerId;
    }
    public String toString(){
        return "GameState{" +
                "players=" + players +
                ", currentPlayerId='" + currentPlayerId + '\'' +
                ", gameEnded=" + gameEnded +
                ", winnerId='" + winnerId + '\'' +
                '}';
    }
}